<?
    /* Se asegura que el archivo es incluido por el index */
    defined( 'LOAD_MODULE_ACTION' ) or die( 'No esta permitido el acceso directo a ' . __FILE__ );
	/* Cuando se hace el include del archivo automaticamente crea un objeto y lo guarda en una variable temporal */
		$this->oTmpObject = new Generico_frm( $this );

	class Generico_frm extends Frm
	{
		var $cDescriptionField;
		var $cPrimary;
	function Generico_frm( &$oModule )
		{
			$this->Frm( $oModule );
			//Crea los Objetos de los campos de edicion
			$this->aFields['cModuleTabla']     			= new InputList( "Tabla", "cModuleTabla" );
			// Arma el conbo con las tablas de configuracion
			$this->aFields['cModuleTabla']->setLabel('Tabla');
		}
	function setLevel( $nLevel)
		{
			switch ( $nLevel )
			{
				case 1:
					$this->aInput = array ( 'cModuleTabla' );
					break;
				case 2:
					$this->aFields['cModuleTabla']->setHidden();
					break;
			}
		}
	function getDescriptionField()
	{
		return $this->cDescriptionField;
	}
	function getPrimaryField()
	{
		return $this->cPrimary;
	}
	function setTableStructure( $cModule, $cTabla )
	{
		$oGenerador = $this->oModule->loadClass( 'generador_exe' );
		$oModule =& loadModule($cModule);
		//$aTabla = $oGenerador->getTableStructure( $oModule->getPath(), $cTabla );
		$aTabla = $oModule->oDatabase->getTableStructure( $cTabla );
		$this->cPrimary  = '';

		foreach ($aTabla  as $aCampo )
		{
			if ( $aCampo['isForeignKey'] )
			{
				$this->aFields[$aCampo['Name']] = new InputList('', $aCampo['Name']);
				$cSql = "SELECT * FROM " . $aCampo['ForeignTable'] ;
				$this->aFields[$aCampo['Name']]->setMatrix( $oModule->oDatabase->getMatrix($cSql));
			} else
			{
				switch( strtolower($aCampo['Type']) )
				{
					case 'char':
					case 'varchar':
					case 'mediumtext':
					default:
						if ( $this->cDescriptionField == '' ) $this->cDescriptionField = $aCampo['Name'] ;
						$this->aFields[$aCampo['Name']] = new InputText('', $aCampo['Name']);
						if ( strtolower($aCampo['Type']) == 'mediumtext' )
							$this->aFields[$aCampo['Name']]->setEditor();
						$this->aFields[$aCampo['Name']]->setLength($aCampo['Length']);
						break;
					case 'numeric':
					case 'decimal':
						$this->aFields[$aCampo['Name']] = new InputNumber('', $aCampo['Name']);
						$this->aFields[$aCampo['Name']]->setLength($aCampo['Length']);
						break;
					case 'datetime':
					case 'date':
						$this->aFields[$aCampo['Name']] = new InputDate('', $aCampo['Name']);
						break;
				}
			}
			if ( $aCampo['isPrimary']  )
			{
				if ( !$aCampo['isForeignKey'] ) $this->aFields[$aCampo['Name']]->setHidden();
				$this->cPrimary .= ',' . $aCampo['Name'];
			}
			//Obtiene el Label desde el nombre del campo, sacando el prefijo y agregando un espacio antes de una Mayuscula ej ncCategoriaIva = Categoria Iva
			for( $nIndex = 0, $nLength = strlen($aCampo['Name']) , $cLabel=''; $nIndex < $nLength; $nIndex++ )
			{
				$cChar = substr($aCampo['Name'], $nIndex, 1);
				if ( $cLabel != '' || ord($cChar) < 97 )
				{
					if ( ord($cChar) >= 97 ) $cLabel .= $cChar;
					else $cLabel .= ' ' . $cChar;
				}
			}
			$this->aFields[$aCampo['Name']]->setLabel($cLabel);
			if ( $this->isMandatory()  ) $this->aFields[$aCampo['Name']]->setMandatory($aCampo['isMandatory']);
			$this->aFields[$aCampo['Name']]->setDefault($aCampo['DefaultValue']);
			if ( $aCampo['Comments'] )
			{
				$aComments = explode(',', $aCampo['Comments'] );
				foreach( $aComments as $cComment )
				{
					switch( trim($cComment) )
					{
						case 'Multilanguage':
							$this->setMultilanguage();
							$this->aFields[$aCampo['Name']]->setMultilanguage();
							break;
						default:
							$this->aFields[$aCampo['Name']]->setLabel($cComment);
							break;
					}
				}
			}
		}
		$this->cPrimary = substr($this->cPrimary , 1);
	}

	function setValue( $aValues )
		{
			list( $cModule, $cTabla ) = explode ( '|', $this->aFields['cModuleTabla']->getValue() );
			$cSql = "SELECT * FROM $cTabla ";
			foreach ( $aValues as $cKey => $cValue )
			{
				$cWhere .=  " AND $cKey = '$cValue'  ";
			}
			$aDatos = $this->oDatabase->getArray($cSql  . ' WHERE ' . substr($cWhere, 5));
			$this->setFieldsArray ( $aDatos );
		}

	function initializeForm()
		{
			global $aConfigTables, $aInstallModules;
			$cFiltro = getParam('cFiltro');
			$cFiltroModule = getParam('cFiltroModule');
			if ( $cFiltroModule  ) 
			{
				$cPathMod = $aInstallModules[$cFiltroModule]['cPathModule'];
				if ( file_exists( $cPathMod . '/tabla_var.php' ) )
					include_once( $cPathMod . '/tabla_var.php' );
			} else
			{
				foreach($aInstallModules as $aModulo )
				{
					$cPathMod = $aModulo['cPathModule'];
					if ( file_exists( $cPathMod . '/tabla_var.php' ) )
						include_once( $cPathMod . '/tabla_var.php' );
				}
			}			
			$cTablaFiltro = getParam( 'cModuleTabla', $_SESSION['cModuleTabla']);
			$cTablaSeleccionada ='';
			foreach( $aConfigTables as $cKey => $aConfig )
			{
				if ( $cFiltro =='' || $cFiltro == $cKey)
				{
					foreach( $aConfig as $cTabla  => $cNombre )
					{
						// Si no hay filtro toma el primero
						if ( $cTablaSeleccionada =='' )  $cTablaSeleccionada = $cTabla;
						// Si encontro el filtro => lo guarda
						if ( $cTablaFiltro== $cTabla )  $cTablaSeleccionada = $cTablaFiltro;
						list( $cTablaModule, $cTablaNombre ) = explode ( '|', $cTabla);
						$aRow = $this->oDatabase->getArray("select * from $cTablaNombre ");
						if (  is_array($aRow) )
							$this->aFields['cModuleTabla']->addOption(  $cTabla, $cNombre);
					}
				}
			}
			$this->aFields['cModuleTabla']->setValue( $cTablaSeleccionada );
			$_SESSION['cModuleTabla'] = $cTablaSeleccionada;
			list( $cModule, $cTabla ) = explode ( '|', $cTablaSeleccionada);
			if ( $cModule ) $this->setTableStructure( $cModule, $cTabla );
			// Si Recibe la Pk => busca los datos
			if ( $this->cPrimary) 
			{
				$aPrimary = explode ( ',',  $this->cPrimary);
				$bSetValues = false;
				foreach( $aPrimary as $cField )
				{
					if ( $this->aFields[$cField]->getValue()  )
					{
						$aValues[$cField] = $this->aFields[$cField]->getValue() ;
						$bSetValues = true;
					}
				}
				if ( $this->isRetrievable() && $bSetValues )
				{
					$this->setValue( $aValues );
				}
			}
			// Configura el Titulo
				$this->setLabel( "Datos" );
			return true;
		}
	}
?>